package com.yang.booking.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.booking.dao.CourseMapper;
import com.yang.booking.dao.ScoreMapper;
import com.yang.booking.dao.StudentMapper;
import com.yang.booking.dao.TeacherMapper;
import com.yang.booking.pojo.Course;
import com.yang.booking.pojo.Score;
import com.yang.booking.pojo.Student;
import com.yang.booking.pojo.StudentScore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CountDownLatch;

@Service
public class StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ScoreMapper scoreMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    public List<Student> queryAll(String id, String name, String birth, String sex){ //, String coursename, String score

        QueryWrapper<Student> wrapper = new QueryWrapper<>();

        if (!id.equals("")){
            wrapper.eq("s_id", id);
        }

        if (!name.equals("")){
            wrapper.eq("s_name", name);
        }

        if (!birth.equals("")){
            wrapper.likeRight("s_birth", birth);
        }

        if (!sex.equals("")){
            wrapper.eq("s_sex", sex);
        }

//        if (!coursename.equals("")){
//            wrapper.eq("c_name", coursename);
//        }
//
//        if (!score.equals("")){
//            wrapper.eq("score", score);
//        }

        //wrapper.likeRight("s_birth ", "1990").eq("s_sex", "男");

        //Page<StudentScore> page = new Page<>(1,3);

        Page<Student> page = new Page<>(1,3);
        studentMapper.selectPage(page, wrapper);
        List<Student> sList = page.getRecords();
        for (Student s: sList) {

//            HashMap<String, Object> map = new HashMap<>();
//            map.put("s_id", s.getsId());
//            s.setScores(scoreMapper.selectByMap(map));

            s.setCourses(courseMapper.queryCourseList(s.getsId()));
            for (Course c:s.getCourses()
                 ) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("s_id", s.getsId());
                map.put("c_id",c.getcId());

                c.setTeacher(teacherMapper.QueryTeacher(c.getcId()));

                c.getTeacher().setCourses(courseMapper.queryCourses(c.getTeacher().gettId()));

                QueryWrapper<Score> swrapper = new QueryWrapper<>();
                swrapper.eq("s_id", s.getsId()).eq("c_id",c.getcId());
                c.setScore(scoreMapper.selectOne(swrapper));
            }
        }

        System.out.println(page.getTotal());
        System.out.println(page.getCurrent());
        System.out.println(page.getSize());

//        List<Student> sList = studentMapper.selectList(null);
//        sList.forEach(System.out::println);

//        List<Student> sList = studentMapper.selectBatchIds(Arrays.asList("02", "03", "04"));

//        List<Student> sList = new ArrayList<>();
//        Student s = studentMapper.selectById("01");

//        HashMap<String, Object> map = new HashMap<>();
//        map.put("s_name", "吴兰");
//        map.put("s_sex", "女");
//        List<Student> sList = studentMapper.selectByMap(map);

//        sList.add(s);

        return sList;
    }

    public List<StudentScore> queryScore(){

        return studentMapper.queryScore();
    }

    public Map<String, Object> insertStudent(Student s){

        studentMapper.insert(s);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入成功");
        msg.put("code", 200);
        return msg;
    }

    public Map<String, Object> updateStudent(String id, String name, String birth, String sex){

        Student s = new Student();
        s.setsId(id);
        s.setsName(name);
        s.setsBirth(birth);
        s.setsSex(sex);

        System.out.println("学生" + s);

        studentMapper.updateById(s);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "更新成功");
        msg.put("code", 200);
        return msg;
    }

    public Map<String, Object> deleteStudent(String id) throws InterruptedException {

        Student s = new Student();
        s.setsId(id);

        System.out.println("学生Id" + s.getsId());
//        studentMapper.deleteById(s.getsId());

//        studentMapper.deleteBatchIds(Arrays.asList("09", "08", "07"));

        HashMap<String, Object> map = new HashMap<>();
        map.put("s_name", "王菊");

        studentMapper.deleteByMap(map);

//        CountDownLatch latch = new CountDownLatch(1);
        new Thread(()->{
            String[] ids = new String[]{"02", "04", "06"};
            for (int i = 0; i < ids.length; i++) {
                studentMapper.deleteById(ids[i]);
            }
//            latch.countDown();
        System.out.println("子线程。。。。。。。。。。。。。。。。。。。");
        }).start();

//        latch.await();

        System.out.println("主线程。。。。。。。。。。。。。。。。。。。");
        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "删除成功");
        msg.put("code", 200);

        return msg;
    }
}
